<?php
require_once("../inc/function.php");

function GetCustomers()
{
        $conn = GetConnection();
        return $conn->query('SELECT * FROM Customer');
}

function GetCustomer($id)
{
        $conn = GetConnection();
		if($id == null)	
			die("No Customer was found with the prodived id or the id is invalid");
        $results = $conn->query("SELECT * FROM Customer WHERE id='$id'");   
        $row = $results->fetch_assoc();
        $conn->close();
        return $row;
}

function SaveCustomer($row)
{	
        $conn = GetConnection();
		$id =$row['id'];
		$first = $row['FirstName'];   
		$last = $row['LastName'];
		$keyword = $row['Keyword_id'];
		if((strlen($first) > 0) && (strlen($last) > 0))
		{			
	        $results = $conn->query("UPDATE Customer SET updated_at=CURRENT_TIMESTAMP, FirstName='$first', LastName='$last', Customer_keywords_id='$keyword' WHERE id='$id' ");
	        ($sql);       
		}
		else 
		{				
            header("Location: http://cs.newpaltz.edu/~N01852322/Final/Customers/edit.php?id=".$id."");
		}
        $conn->close();    
}

function NewCustomer($row){
$first_name = strip_tags($row['first_name']);
$last_name = strip_tags($row['last_name']);
$date = date("Y:m:d H:m:s");
$keyword_id = 3;

	if((strlen($first_name) > 0) && (strlen($last_name) > 0))
	{
		$conn = GetConnection();
		$conn->query("INSERT INTO Customer Values (NULL,'$date','CURRENT_TIMESTAMP','$first_name','$last_name','$keyword_id')");
		$conn->close();		
	}
	else 
	{
		die("You need to enter a customer name.");
	}
}

function DeleteCustomer($id)
{
	$conn = GetConnection();
	$results = $conn->query("DELETE FROM Customer WHERE id=$id");
	header('Location: http://cs.newpaltz.edu/~N01852322/Final/Customers/');
}
?>